// 
//  ProcessUtil.cs
//  
//  Author:
//       Nikita Tsukanov <keks9n@gmail.com>
// 
//  Copyright (c) 2011 Nikita Tsukanov
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 


using System;
namespace SkypeTab
{
	public static class ProcessUtil
	{
		public static string ExecuteRead(string Exe, string CmdLine)
		{
			return ExecuteRead(Exe, CmdLine,null);	
		}
		public static string ExecuteRead(string Exe, string CmdLine, string Display)
		{
			var proc=Start(Exe,CmdLine,true,false,false,Display);
			proc.WaitForExit();
			return proc.StandardOutput.ReadToEnd();
		}
		public static void ExecuteWait(string Exe, string CmdLine)
		{
			var proc=Start(Exe,CmdLine,false,false,false,null);
			proc.WaitForExit();
		}
		
		static string LibFixPath=null;
		
		public static System.Diagnostics.Process Start(string Exe, string CmdLine, bool RedirectStdOut,
		 bool RedirectStdErr, bool RedirectStdIn, string Display)
		{
			var nfo=new System.Diagnostics.ProcessStartInfo(Exe,CmdLine);
			nfo.UseShellExecute=false;
			
			//Find dispfix.so
			if(LibFixPath==null)
			{
				LibFixPath=System.Configuration.ConfigurationManager.AppSettings["dispfixpath"];
				if(LibFixPath==null)
				{
					LibFixPath=System.Reflection.Assembly.GetExecutingAssembly()
						.GetModules()[0].FullyQualifiedName;
					LibFixPath=System.IO.Path.GetDirectoryName(LibFixPath);
					LibFixPath=System.IO.Path.Combine(LibFixPath,"dispfix.so");
				}
			}
			
			
			//Note: we're using dispfix.so because we don't want the browser 
			//(which will be launched by skype) to open it's windows inside our display
			if(Display!=null)
			{
				if(!System.IO.File.Exists(LibFixPath))
					throw new Exception("Unable to find dispfix.so");
				nfo.EnvironmentVariables.Clear();
				
				nfo.EnvironmentVariables.Add("ST_DISPLAY", Display);
				nfo.EnvironmentVariables.Add("ST_EXE", Exe);
				nfo.EnvironmentVariables.Add("LD_PRELOAD",LibFixPath);
				
				
				foreach(System.Collections.DictionaryEntry v in Environment.GetEnvironmentVariables())
				{
					if(!nfo.EnvironmentVariables.ContainsKey((string)v.Key))
						nfo.EnvironmentVariables.Add((string)v.Key, (string)v.Value);
				}
				
			}
			nfo.RedirectStandardError=RedirectStdErr;
			nfo.RedirectStandardInput=RedirectStdIn;
			nfo.RedirectStandardOutput=RedirectStdOut;
			return System.Diagnostics.Process.Start(nfo);
		}
	}

}

